package cn.edu.ctbu.dp3.service;

import cn.edu.ctbu.dp3.dao.StudentRepository;
import cn.edu.ctbu.dp3.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {
    @Autowired
    public StudentRepository studentRepository;

    public List<Student> getAll() {
        return studentRepository.findAll();

    }
//    按id进行查询
    public Student findById(Integer id){
        return studentRepository.findById(id).orElse(null);
    }


    //    按名字进行查询，like操作
    public List<Student> findByName(String name){
        return studentRepository.findByNameLike(name);
    }

    /**
     * @param name
     * @param password
     * @return   idea自动生成
     */
    //名字和密码
    public List<Student> findByNameAndPassword(String name,String password){
        return studentRepository.findByNameAndPassword(name,password);
    }

    public void  insert(Student student){
        studentRepository.save(student);
    }

//    public void  update(Student student){
//        studentRepository.save(student);
//    }

//    public void  delete(Integer id){
//        studentRepository.deleteById(id);
//    }
    public Student add(Student student){
        return studentRepository.save(student);
    }

    public Student update(Student student){
        return studentRepository.save(student);
    }

    public void delete(Integer id){
         studentRepository.deleteById(id);
    }





}
